<template>
  <div class="alarm-management">
    <!-- 搜索 -->
    <div class="form_search">
      <el-form @submit.native.prevent>
        <el-input v-model="imei" placeholder="在此输入IMEI编号进行搜索" clearable></el-input>
      </el-form>
      <el-button type="primary" slot="append" class="dev_search" @click="dev_search">搜索</el-button>
    </div>
    <!-- 搜索 -->
    <search @search_data="search_data"></search>
    <!-- 列表 -->
    <el-table
      :data="listData"
      :header-cell-style="getRowClass"
      v-loading="loading"
      stripe
      id="table"
    >
      <el-table-column prop="num" label="序号"></el-table-column>
      <el-table-column prop="devName" label="设备类型"></el-table-column>
      <el-table-column prop="fireplugId" label="位置编号"></el-table-column>
      <el-table-column prop="fireAdress" label="设备位置" min-width="150%">
        <template slot-scope="scope">
          <span :title="scope.row.fireAdress">{{ scope.row.fireAdress}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="starttime" label="开始时间"></el-table-column>
      <el-table-column prop="stoptime" label="结束时间"></el-table-column>
      <el-table-column prop="time" label="报警时长"></el-table-column>
      <el-table-column prop="onlineState" label="设备状态">
        <template slot-scope="scope">
          <span>{{ scope.row.onlineState==='ONLINE'?'设备在线':'设备离线'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="报警类型">
        <template slot-scope="scope">
          <span
            :style="scope.row.type === 0? 'color:#07CCD3':scope.row.type === 1? 'color:#07CCD3': scope.row.type === 2? 'color:#EB3838': scope.row.type === 3? 'color:#A141FF': scope.row.type === 4? 'color:#07CBD2': ''"
          >{{ scope.row.type === 0? '水压异常报警 ' + scope.row.wpdata + 'MPa':scope.row.type === 1? '水压异常报警 ' + scope.row.wpdata + 'MPa': scope.row.type === 2? '低电量 ':scope.row.type === 3? '传感器异常 ': scope.row.type === 4? '烟感报警': ''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            title="查看"
            @click="handelSee(scope.row)"
            style="width:0px;padding:0;"
            v-has-permission="'alarm_query'"
          >
            <img src="../../../../static/image/device/see.png">
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <div class="page-left">
        <span>共{{ this.totals}}条数据</span>
        <span>每页:</span>
        <el-input
          placeholder="条数"
          v-model="inputs"
          @blur="enter"
          @keyup.enter.native="enter"
          clearable
        ></el-input>
        <span>条</span>
      </div>
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :page-size="pageSize"
        :total="totals"
        @current-change="handleCurrentChanges"
        id="page"
        :current-page.sync="currentPage"
      ></el-pagination>
    </div>

    <!-- Form 弹框查看 -->
    <el-dialog
      :close-on-click-modal="false"
      title="设备预览"
      :visible.sync="dialogTableVisible"
      :modal-append-to-body="false"
      class="dialog-see"
      v-if="dialogTableVisible"
    >
      <div class="left-box">
        <!-- 进度条 -->
        <div class="facility">
          <div style="height:100%;width:100%;float:right;position: relative;">
            <div
              style="float:left;"
              v-if="form.devname === 'fireplug' && +form.wpdata < form.wplowthd/1000"
            >
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; left: 27px; color: #999; font-size: 12px;"
              >水压状态异常</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 20%;left: 1%;width: 110px;text-align: center;"
              >
                {{form.wpdata}}
                <span style="font-size:16px; color: #999999;">MPa</span>
              </div>
            </div>
            <div
              style="float:left;"
              v-if="form.devname === 'fireplug'  && +form.wpdata > form.wplowthd/1000"
            >
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; left: 7px; color: #999; font-size: 14px;width: 110px;text-align: center;"
              >水压在线</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%; left: 1%;width:110px;text-align:center"
              >
                {{form.wpdata}}
                <span style="font-size:16px; color: #999999;">MPa</span>
              </div>
            </div>
            <!-- <div style="float:left;" v-if="form.devname === 'scm' && form.alarmtype === '无警情'">
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; left: 27px; color: #999; font-size: 12px;"
              >环境温度{{form.temp}}℃</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;left: 1%;width: 110px;text-align: center;"
              >无警情</div>
            </div>-->
            <div style="float:left;" v-if="form.devname === 'scm' ">
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; left: 27px; color: #999; font-size: 12px;"
              >环境温度{{form.temp}}℃</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;left: 1%;width: 110px;text-align: center;"
              >烟感报警</div>
            </div>
            <span
              style="position: absolute; top: 80%; left: 34px; color: #999; font-size: 14px;"
            >警情状态</span>

            <div style="float:left;margin-left:70px;" v-if="form.volt < 10">
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; left: 30%; color: #999; font-size: 12px;"
              >当前电池状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;left: 27%;width: 110px;text-align: center;"
              >请更换电池</div>
            </div>
            <div style="float:left;margin-left:70px;" v-if="form.volt >10 && form.volt <20">
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; left: 26%; color: #999; font-size: 12px;"
              >当前电池状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;left: 25%;width: 110px;text-align: center;"
              >电量低</div>
            </div>
            <div style="float:left;margin-left:70px;" v-if="form.volt  > 20">
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; left: 28%; color: #999; font-size: 12px;width: 100px;text-align: center;"
              >当前电池状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%; left: 27%;width:120px;text-align:center"
              >电量充足</div>
            </div>
            <span
              style="position: absolute; top: 80%; left: 30%; color: #999; font-size: 14px;"
            >电池状态</span>

            <div style="float:left;margin-left:70px;" v-if="form.onlineState === 'OFFLINE'">
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; left: 56%; color: #999; font-size: 12px;"
              >当前在线状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;left: 53%;width: 118px;text-align: center;"
              >设备离线</div>
            </div>
            <div style="float:left;margin-left:70px;" v-if="form.onlineState === 'ONLINE'">
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; left: 53%; color: #999; font-size: 12px;width: 110px;text-align: center;"
              >当前在线状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%; left: 53%;width:110px;text-align:center"
              >设备在线</div>
            </div>
            <span
              style="position: absolute; top: 80%; left: 57%; color: #999; font-size: 14px;"
            >在线状态</span>

            <div
              style="float:left;margin-left:70px;"
              v-if="typeof(form.alert) === 'number'&&form.sensor !== '传感器正常'"
            >
              <img src="/static/image/alarm/off.png">
              <span
                style="position: absolute; top: 44%; right:7.8%; color: #999; font-size: 12px;"
              >当前传感器状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%;width: 127px;text-align: center;"
              >传感器异常</div>
            </div>
            <div
              style="float:left;margin-left:70px;"
              v-if="typeof(form.alert) === 'number'&&form.sensor === '传感器正常'"
            >
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; right:6%;color: #999; font-size: 12px;width: 110px;text-align: center;"
              >当前传感器状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%; width:120px;text-align:center"
              >传感器正常</div>
            </div>

            <div style="float:left;margin-left:70px;" v-if="typeof(form.alert) !== 'number'">
              <img src="/static/image/alarm/on.png">
              <span
                style="position: absolute; top: 44%; right:6%;color: #999; font-size: 12px;width: 110px;text-align: center;"
              >当前传感器状态</span>
              <div
                style="position: absolute;font-size: 20px; color: #333; top: 25%; width:120px;text-align:center"
              >--</div>
            </div>
            <span
              style="position: absolute; top: 80%; right:8%; color: #999; font-size: 14px;"
            >传感器状态</span>
          </div>
        </div>
        <div style="border-bottom: 1px dashed #efeaea;margin-top:12px;"></div>

        <div style="width:100%;">
          <div style="margin:30px 0;font-size:16px;color:#333;">设备信息</div>
          <el-form
            :model="form"
            @submit.native.prevent
            label-width="100px"
            style="padding-left:6px;"
          >
            <div style="font-size:14px;color:#333333;height:16px;line-height:16px">
              <span style="width:33%;float:left;">设备编号：{{form.imei}}</span>
              <span style="width:33%;float: left;">所在区域：{{form.areaName}}</span>
              <span style="width:33%;float: left;">负责人姓名：{{form.name}}</span>
            </div>

            <div style="font-size:14px;color:#333333;margin-top:28px;height:16px;line-height:16px">
              <span style="width:33%;float:left;">位置编号：{{form.fireplugId}}</span>
              <span style="width:33%;float:left;">场所名称：{{form.siteName}}</span>
              <span style="width:33%;float:left;">负责人电话：{{form.phone}}</span>
            </div>
            <div style="font-size:14px;color:#333333;margin-top:28px;height:16px;line-height:16px">
              <span style="width:33%;float:left;" v-if="form.devname =='fireplug'">设备类型：消防栓</span>
              <span style="width:33%;float:left;" v-if="form.devname =='scm'">设备类型：烟感设备</span>
              <span
                style="width:33%;float:left;"
                v-if="form.devname =='fireplug'"
              >详细地址：{{form.siteAddress}}</span>
              <span
                style="width:33%;float:left;"
                v-if="form.devname =='scm'"
              >详细地址：{{form.siteAddress}}</span>
              <span style="width:33%;float:left;" v-if="form.devname =='scm'">复核人姓名：{{form.opertor}}</span>
            </div>
            <div style="font-size:14px;color:#333333;margin-top:28px;height:16px;line-height:16px" v-if="form.devname =='scm'">
              <span style="width:33%;float:left;">复核结果：{{form.content}}</span>
            </div>
          </el-form>
        </div>
        <div style="border-bottom: 1px dashed #efeaea;padding:12px 0"></div>

        <!-- 下方 -->
        <div class="message">
          <div style="font-size:16px;color:#333333;margin-top: 28px;">历史报警记录</div>

          <div style="width:100%;height: 200px;margin-top: 30px;padding-left:6px;">
            <vue-scroll @handle-scroll="handleComplete" :ops="ops">
              <!-- 列表 -->
              <el-table
                :data="listDataS"
                :header-cell-style="getRowClass"
                v-loading="loading"
                stripe
              >
                <el-table-column prop="num" label="序号"></el-table-column>
                <el-table-column prop="time" label="报警时间"></el-table-column>
                <el-table-column prop="types" label="报警类型"></el-table-column>
              </el-table>
            </vue-scroll>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import moment from 'moment'
import echarts from 'echarts'
import Config from '../../../config'
import TimeSelect from '../../../components/TimeSelect'
import Search from './Search'
import Location from '../../../util/location'
import qs from "qs"
// import { regionDataPlus } from 'element-china-area-data'
import { regionDataPlus } from 'static/js/regionData'
export default {
  name: 'AlarmManagement',
  components: {
    TimeSelect,
    Search
  },
  data: function () {
    return {
      ops: {
        bar: {
          background: '#DDDEE0',
          onlyShowBarOnScroll: false,
        }
      },
      // 通过imei搜索
      imei: '',
      //列表
      listData: [],
      // loading
      loading: false,
      // 分页
      totals: null,
      currentPage: 1,
      pageSize: 15,
      inputs: 15,
      // 弹框
      dialogTableVisible: false,
      // 弹框数据
      form: {
        // name: '',
        // phone: ''
      },
      // 弹框列表
      listDataS: [],
      // 列表条数
      num: 0,
      // 滚动之后请求的页数
      flag_number: 0,

      // 搜索条件
      obj: null,

      // 判断是路由还是正常请求
      flag: 0,

      // 定时器
      // IntervalList: null
    }
  },

  methods: {
    // 表头添加背景颜色
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#f6f6f6'
      } else {
        return ''
      }
    },
    // 查看
    async handelSee(data) {
      // 判断位数 二进制转换
      if (typeof (data.alert) === 'number') {
        let type = data.alert.toString(2).padStart(8, "0").split("").reverse();
        for (var b = 0; b < type.length; b++) {
          if (type[b] === "0") {
            switch (b) {
              case 0:
                data.alarmtype = '无警情'
                break;
              case 3:
                data.sensor = '传感器正常'
                break;
            }
          }
        }
      }
      this.form = JSON.parse(JSON.stringify(data))
      if (this.form.devname ==='scm') {
        let tmp = await this.getSilenceByAlarmId(this.form.fireplugAlarmId)
        this.form.opertor = tmp.opertor
        this.form.content = tmp.content
      }
      this.dialogTableVisible = true
      this.num = 0
      this.flag_number = 0
      this.listDataS.splice(0)

      this.$nextTick(() => {
        // 列表
        this.window_list(0)
      })
      // 管理人员
      this.people(data.siteId)
    },
    getSilenceByAlarmId  (id) {
      let url = `${Config.FIREPLUG_APP_URL}/nbiotSilence/getSilenceByAlarmId?alarmId=${id}`
      return new Promise((resolve, reject) => {
        this.$http.get(url)
          .then(data => {
            if (data) {
              resolve(data)
            } else {
              resolve({opertor: '', content: ''})
            }
          })
          .catch(e => resolve({opertor: '', content: ''}))
      })
    },
    // 管理员信息
    people(id) {
      let url = `${Config.AUTH_URL}/getManagerInfoByDistrictId`
      let param = {
        districtId: id
      }
      this.$http.get(url, param).then((res) => {
        // console.log(res)
        this.$nextTick(() => {
          this.form.name = res.name
          this.form.phone = res.phone
        })

      }).catch(e => {
        console.log(e)
      })
    },
    // 弹框列表
    window_list(page) {
      let url = `${Config.FIREPLUG_APP_URL}/fireplug/alarm/${this.form.imei}`
      let param = {
        page: page,
        size: 5
      }
      this.$http.get(url, param).then((res) => {
        res.content.map((item, i) => {
          item.num = this.num + 1 + i
          item.time = moment(item.ts).format('YYYY.MM.DD HH:mm:ss')
          item.types = item.type === 0 ? '水压低压报警' : item.type === 1 ? '水压高压报警' : item.type === 2 ? '电池低压报警' : item.type === 3 ? '传感器异常' : item.type === 4 ? '烟感报警' : ''
        })
        this.listDataS = this.listDataS.concat(res.content)
        this.num += res.content.length
      })
    },
    // 弹框列表滚动条
    handleComplete(vertical) {
      if (vertical.process === 1) {
        this.flag_number++
        this.window_list(this.flag_number)
      }
    },

    // 搜索条件
    search_data(datas) {
      // 如果没用路由
      if (this.flag) {
        // console.log(datas)
        this.currentPage = 1
        let data = JSON.parse(JSON.stringify(datas))

        let obj = {}

        // 设备条件
        switch (data.devname) {
          case '消防栓':
            obj.devname = 'fireplug'
            break
          case '烟感设备':
            obj.devname = 'scm'
            break
        }
        // 报警条件
        switch (data.alarm) {
          case '警情异常':
            if (data.devname === '') {
              obj.wiStatus = 1 // 警情异常
            }
            if (data.devname === '消防栓') {
              obj.wpStatus = 1 // 水
            }
            if (data.devname === '烟感设备') {
              obj.smokeStatus = 1 // 烟雾
            }
            break
          case '传感器异常':
            obj.sensorStatus = 1
            break
          case '低电量':
            obj.voltStatus = 1
            break
          case '设备离线':
            obj.online_state = 'OFFLINE'
            break
        }

        // 城市
        if (data.number.district) {
          obj.site_id = data.number.district
        } else if (data.number.area) {
          obj.district = data.number.area
        } else if (data.number.city) {
          obj.city = data.number.city
        } else if (data.number.province) {
          obj.province = data.number.province
        } else {
          obj.country = data.number.country
        }


        this.obj = JSON.parse(JSON.stringify(obj))
        this.getList(this.obj)

      } else {
        this.rout()

        this.flag = 1
      }
    },
    // 通过imei
    dev_search() {
      this.currentPage = 1
      this.getList(this.obj)
    },
    getList(params) {
      this.loading = true
      // if (this.IntervalList) {
      //   clearInterval(this.IntervalList)
      // }
      // 判断权限 test//神//凡人
      if (this.role[0].sort === 93) {
        params.site_type = 'test'
      } else if (this.role[0].sort !== 100) {
        params.site_type = 'official'
      }
      params.imei = this.imei ? this.imei : undefined
      params.latest = false
      params.subtype = "collective"
      params.sort = 'stop_time desc'

      let url = `${Config.FIREPLUG_APP_URL}/fireplug/alarm/search?page=${(this.currentPage - 1) * this.pageSize}&size=${this.pageSize}`
      this.$http.post(url, params).then((res) => {
        this.dispose(res)
      })
    },
    handleData(item) {
      const province = this.getAreaObj(regionDataPlus, item.province)
      if (item.city === '419000' || item.district === '419001' || item.district === '济源市') {
        item.areaName = `${province.label}济源市`
      } else {
        const city = this.getAreaObj(province.children, item.city)
        const district = item.district ? this.getAreaObj(city.children, item.district) : { label: '' }
        item.areaName = `${province.label}${city.label}${district.label}`
      }
    },
    /**
     * 获取区域信息
     * @param {Array} arr
     * @param {String} value
     * @return {Object} obj
     * */
    getAreaObj(arr, value) {
      let obj = null
      arr.some(item => {
        if (value === item.value) {
          obj = item
          return true
        }
      })
      return obj
    },

    // 路由传递
    rout() {
      let obj = {}
      obj.fireplugAlarmId = this.$route.params.id.fireplugAlarmId

      this.obj = JSON.parse(JSON.stringify(obj))
      this.getList(this.obj)

    },

    // 数据处理
    dispose(res) {
      this.totals = res.total
      this.loading = false
      if (res.content.length) {
        res.content.map((item, i) => {
          if (this.currentPage === 1) {
            item.num = i + 1
          } else {
            item.num = (this.currentPage - 1) * this.pageSize + i + 1
          }
          switch (item.devname) {
            case "fireplug":
              item.devName = '消防栓'
              break
            case "scm":
              item.devName = '烟感设备'
              break
          }
          this.handleData(item)
          item.fireAdress = item.areaName + item.siteName + item.siteAddress
          item.starttime = item.startTime ? moment(item.startTime).format('YYYY.MM.DD HH:mm:ss') : ''
          item.stoptime = item.stopTime ? moment(item.stopTime).format('YYYY.MM.DD HH:mm:ss') : ''
          if (item.startTime && item.stopTime) {
            let du = moment.duration(moment(item.stopTime) - moment(item.startTime), 'ms')
            let hours = du.get('hours')
            let mins = du.get('minutes')
            let ss = du.get('seconds')

            if (hours) {
              item.time = hours + '时' + mins + '分' + ss + '秒'
            } else {
              item.time = mins + '分' + ss + '秒'
            }

          } else {
            item.time = ''
          }
        })
        this.listData = res.content

      } else {
        this.listData = res.content
        this.loading = false
      }
    },
    // 回车事件
    enter() {
      if (+this.inputs === 0) {
        this.inputs = 15
      }
      if (Number.isInteger(+this.inputs)) {
        this.pageSize = +this.inputs
        this.currentPage = 1
        this.getList(this.obj)
      } else {
        this.$message({
          message: "请输入整数",
          type: "warning"
        })
      }
    },
    // 分页
    handleCurrentChanges(val) {
      this.getList(this.obj)
    }
  },
  computed: {
    ...mapGetters('user', {
      module: 'dealedModule',
      user: 'getUser',
      role: 'getRole',
      district: 'getDistrict'
    })
  },
  mounted() {
    // this.get_list(0, this.pageSize)

    // 如果没路由 则不让请求路由传递的数据
    if (!this.$route.params.id) {
      this.flag = 1
    }
  },
  // destroyed() {
  //   clearInterval(this.IntervalList)
  // },
}
</script>
<style lang="scss">
@import "../../../../static/scss/common";
@import "../../../../static/scss/design-list";
.alarm-management {
  // 搜索
  .form_search {
    width: 100%;
    height: 42px;
    margin: 4px 0 16px;
    .el-form {
      width: 500px;
      float: left;
      height: 42px;
      line-height: 42px;
      box-shadow: 5px 5px 0px 0px rgba(34, 34, 34, 0.1);
      border: 0;
      .el-input {
        .el-input__inner {
          height: 42px;
          border-radius: 0;
          border: 0;
          line-height: 42px;
        }
        .el-input__inner:hover {
          border-color: 0;
        }
      }
    }
    .dev_search {
      width: 80px;
      height: 42px;
      background: rgba(235, 56, 56, 1);
      box-shadow: 5px 5px 0px 0px rgba(34, 34, 34, 0.1);
      padding: 0;
      border-radius: 0;
      font-size: 14px;
    }
  }
  // 分页
  .pagination {
    padding: 32px 0 44px 23px;
    background: #fff;
    .page-left {
      font-size: 12px;
      display: inline-block;
      .el-input--suffix {
        width: 56px;
      }
    }
  }
  // 弹框
  .dialog-see.el-dialog__wrapper {
    .el-dialog {
      width: 800px;
      height: 830px !important;
      margin-top: auto !important;
      margin: auto;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }
    .el-dialog__body {
      // height: 616px;
      padding: 24px 0;
      box-sizing: border-box;
    }
  }
  .dialog-see {
    .left-box {
      float: left;
      width: 775px;
      height: 570px;
      box-sizing: border-box;
      padding-right: 24px;
      // 进度条
      .facility {
        width: 100%;
        height: 173px;
        padding-left: 20px;
        // 左侧表
        // .el-form-item {
        //   height: 38px !important;
        // }
        // .el-form-item__label {
        //   line-height: 20px !important;
        //   color: $gray3;
        // }
        // .el-form-item__content {
        //   line-height: 20px !important;
        // }
        // 右侧svg
        .el-progress-circle {
          width: 170px !important;
          height: 170px !important;
        }
        .el-progress__text {
          font-size: 32px !important;
          color: $gray3 !important;
          top: 45% !important;
        }
      }
      // 下方
      .message {
        width: 100%;
        height: 340px;
        .el-table {
          padding: 0;
        }
      }
    }
  }
  #table .cell {
    // margin-left: 40px !important;
    // white-space:nowrap;
  }
}
</style>
